<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="添加规则组" :visible.sync="dialogVisible" @close="resetForm('addRuleGroupForm')" width="1200px">
        <div class="over_box">
            <el-form label-position='left' label-width="90px" size="small" :model="addRuleGroupForm" :hide-required-asterisk='true' ref="addRuleGroupForm" :rules="addRuleGroupRules" class="add_form">
                <el-form-item label="规则设置" prop="">
                    <div class="rule_box">
                        <div class="rule_title flexbox flexcenter">
                            设置条件
                            <img src="../../assets/img/tips_icon.svg" alt="" style="margin:0 5px">
                            <span class="font_13 color_03">大于等于左边界，小于等于右边界</span>
                        </div>
                        <div class="cod_item flexbox flexcenter" v-for="(item,index) in addRuleGroupForm.rules" :key="index">
                            <span class="cod_text font_14 color_01 ml_12">如果消耗</span>
                            <el-form-item label="" :prop="`rules.${index}.cost_left`" :rules="addRuleGroupRules.group" class="ml_12">
                                <el-input v-model="item.cost_left" placeholder="请输入" style="width:120px"></el-input>
                            </el-form-item>
                            <span class="cod_text font_14 color_01 ml_12">-</span>
                            <el-form-item label="" :prop="`rules.${index}.cost_right`" :rules="addRuleGroupRules.group" class="ml_12">
                                <el-input v-model="item.cost_right" placeholder="请输入" style="width:120px"></el-input>
                            </el-form-item>
                            <span class="cod_text font_14 color_01 ml_12">且roi</span>
                            <el-form-item label="" :prop="`rules.${index}.roi_left`" :rules="addRuleGroupRules.group" class="ml_12">
                                <el-input v-model="item.roi_left" placeholder="请输入" style="width:120px"></el-input>
                            </el-form-item>
                            <span class="cod_text font_14 color_01 ml_12">-</span>
                            <el-form-item label="" :prop="`rules.${index}.roi_right`" :rules="addRuleGroupRules.group" class="ml_12">
                                <el-input v-model="item.roi_right" placeholder="请输入" style="width:120px"></el-input>
                            </el-form-item>
                            <span class="cod_text font_14 color_01 ml_12">则触发关停</span>
                            <div class="icon_btn ml_12" v-if="addRuleGroupForm.rules.length>1"><img src="../../assets/img/del_icon2.svg" alt="" @click="deletCondition(index)"></div>
                        </div>
                        <el-link type="primary" :underline="false" class="mt_5" @click="addCondition">添加条件</el-link>
                    </div>
                </el-form-item>
            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm('addRuleGroupForm')" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm('addRuleGroupForm')" :loading="loading" class="el-button-width">添 加</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            type:null,
            addRuleGroupForm: {
                video_testing_id:'',
                rules:[
                    {
                        cost_left:'',
                        cost_right:'',
                        roi_left:'',
                        roi_right:'',
                    }
                ]
            },

            addRuleGroupRules: {
                group: [
                    {
                        required: true,
                        message: "请输入",
                        trigger: ["blur", "change"],
                    },
                    {
                        pattern:/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
                        required: true,
                        message: "请输入正确数值",
                        trigger: ["blur", "change"],
                    },
                ],
            },
        };
    },
    created() {
        this.$bus.on("addVideoRules", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.addRuleGroupForm.video_testing_id = value.video_testing_id
            this.getRulesList()
        });
    },
    methods: {

        //获取列表
        async getRulesList() {
            const { data: res } = await this.$http.post(
                "videotest/videotesting/videoTestingRulesList",
                {
                    video_testing_id:this.addRuleGroupForm.video_testing_id
                }
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                if(res.data.length>0){
                    this.addRuleGroupForm.rules = res.data
                }
            }
        },

        // 添加条件
        addCondition() {
            this.addRuleGroupForm.rules.push({
                roi_left: "",
                roi_right: "",
                cost_right: "",
                cost_left: "",
            });
        },

        // 删除条件
        deletCondition(index) {
            this.addRuleGroupForm.rules.splice(index, 1);
        },

        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true
                    const { data: res } = await this.$http.post(
                        "videotest/videotesting/videoTestingRulesEdit",
                        this.addRuleGroupForm
                    );
                    if (res.code !== 200) {
                        this.loading = false
                        this.$message.error(res.msg);
                        return;
                    } else {
                        this.loading = false
                        this.$message({
                            message: "添加成功",
                            type: "success",
                        });
                        this.$parent.getDataList(1);
                        this.resetForm('addRuleGroupForm')
                    }
                } else {
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
    components: {},
};
</script>


<style scoped>
.over_box {
    max-height: 670px;
}
</style>
